package wakeup.server.tcpserver;

import java.io.DataInputStream;
import java.io.DataOutputStream;
import java.io.IOException;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.net.Socket;
import java.util.Date;
import java.util.logging.Level;
import java.util.logging.Logger;

import wakeup.server.model.Client;
import wakeup.server.model.Track;
import wakeup.server.persistence.TrackDAO;

public class ClientHandler implements Runnable {
	
	final Socket socket;
	
	TrackDAO trackDAO;
	
	private final Logger logger = Logger.getLogger("logger");
	
	public ClientHandler(Socket socket, TrackDAO trackDAO)  {
		this.socket = socket;
		this.trackDAO = trackDAO;
	}
		
	@Override
	public void run() {
		
        try {			
			ObjectOutputStream oos = new ObjectOutputStream(this.socket.getOutputStream());
			ObjectInputStream ois = new ObjectInputStream(this.socket.getInputStream());
			
			logger.log(Level.INFO, "------------- Réception des inforamtion du client -------------");
			Date date = (Date) ois.readObject();
			
			logger.log(Level.INFO, "**************  Date demandé : "+date.getDate()+"/"+date.getMonth()+"/"+date.getYear());
			
			Track track = trackDAO.getTrack(date);	
			
			logger.log(Level.INFO, "---------------->>>>>> Titre envoyé : "+track.getTitle());
			
        	oos.writeObject(track);
        	oos.flush();
			
			ois.close();
			oos.close();			 			
			
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (ClassNotFoundException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
        
        
       
        
		
	}

}
